import commentApi from '../../api/commentApi'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    activeId: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    comList: [],
    pageNum: 0,
    pageSize: 8,
    isMore: true
  },

  observers: {
    activeId: function (value) {
      this.mounted()
    }
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {},
    moved: function () {},
    detached: function () {},
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      console.log('comment list show....')
      let addCommentSuccess = wx.getStorageSync('addCommentSuccess')
      if (addCommentSuccess) {
        wx.removeStorageSync('addCommentSuccess')
        this.setData({
          comList: [],
          isMore: true
        })
        this.query(1)
      }
    },
    hide: function () {},
    resize: function () {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    mounted() {
      if (!this.data.activeId) {
        return
      }
      this.query(1)
    },
    async query(num) {
      if (!this.data.isMore) {
        return
      }
      this.data.pageNum = num
      let res = await commentApi.getList({
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize,
        activeId: this.data.activeId
      })

      this.setData({
        comList: this.data.comList.concat(res),
        isMore: res.length == this.data.pageSize
      })

    },
    nextPage() {
      this.query(this.data.pageNum + 1)
    },
    previewImage(e) {
      let index = e.currentTarget.dataset.index
      let list = e.currentTarget.dataset.list
      wx.previewImage({
        urls: list,
        current: list[index]
      })
    }
  }
})